from django.db import models
from django.db.models.query import QuerySet
from django.db.models.query_utils import Q


class TemporalQuerySet(QuerySet):
    def valid_on_date(self, date): 
        return self.filter(
            Q(valid_from__lte=date),
            Q(valid_to__gte=date) | Q(valid_to=None)
        )


class TemporalManager(models.Manager): 
    use_for_related_fields = True

    def get_query_set(self):
        return TemporalQuerySet(self.model)

    def valid_on_date(self, *args, **kwargs):
        return self.get_query_set().valid_on_date(*args, **kwargs)    
